package com.origin.utils.image

import android.content.Context
import android.graphics.drawable.Drawable
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.signature.ObjectKey

import com.origin.utils.time.TimeUtil
import java.io.File

object ImageLoader {
     var timeTag = TimeUtil.getNowTime()


    @JvmStatic
    fun loaderImgLocation(
        context: Context,
        res: Int,
        error: Int,
        load: Int, options: RequestOptions? = null
    ): GlideRequest<Drawable> {

        return if (options != null) {
            GlideApp.with(context).load(res).placeholder(load).error(error).apply(options)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        } else {
            GlideApp.with(context).load(res).placeholder(load).error(error)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        }

    }

    @JvmStatic
    fun loaderImgLocationGif(
        context: Context,
        res: Int,
        error: Int,
        load: Int, options: RequestOptions? = null
    ): GlideRequest<GifDrawable> {

        return if (options != null) {
            GlideApp.with(context).asGif().load(res).placeholder(load).error(error).apply(options)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        } else {
            GlideApp.with(context).asGif().load(res).placeholder(load).error(error)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        }

    }


    @JvmStatic
    fun loaderImgDiyCache(
        context: Context,
        url: String,
        error: Int,
        load: Int,  cacheKey:String ,options: RequestOptions? = null
    ): GlideRequest<Drawable> {

        return if (options != null) {
            GlideApp.with(context).load(url).placeholder(load).error(error).apply(options)
                .skipMemoryCache(false)
                .signature(ObjectKey(cacheKey))
        } else {
            GlideApp.with(context).load(url).placeholder(load).error(error)
                .skipMemoryCache(false)
                .signature(ObjectKey(cacheKey))
        }

    }

    @JvmStatic
    fun loaderImg(
        context: Context,
        url: String,
        error: Int,
        load: Int, options: RequestOptions? = null
    ): GlideRequest<Drawable> {

        return if (options != null) {
            GlideApp.with(context).load(url).placeholder(load).error(error).apply(options)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        } else {
            GlideApp.with(context).load(url).placeholder(load).error(error)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        }

    }

    @JvmStatic
    fun loaderImgFile(
        context: Context,
        urlFile: String,
        error: Int,
        load: Int, options: RequestOptions? = null
    ): GlideRequest<Drawable> {

        return if (options != null) {
            GlideApp.with(context).load( File(urlFile)).placeholder(load).error(error).apply(options)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        } else {
            GlideApp.with(context).load( File(urlFile)).placeholder(load).error(error)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        }

    }

    @JvmStatic
    fun loaderImg(
        context: Context,
        url: String,
        error: Drawable,
        load: Drawable, options: RequestOptions? = null
    ): GlideRequest<Drawable> {

        return if (options != null) {
            GlideApp.with(context).load(url).placeholder(load).error(error).apply(options)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        } else {
            GlideApp.with(context).load(url).placeholder(load).error(error)
                .skipMemoryCache(false)
                .signature(ObjectKey(timeTag))
        }

    }


}